package com.mark.web.stars.controller;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.mark.common.CurrentUser;
import com.mark.common.ServerResponse;
import com.mark.common.utils.DateTimeUtil;
import com.mark.web.common.entity.SelectVo;
import com.mark.web.common.service.IUploadFilesService;
import com.mark.web.stars.entity.from.AddFragmentFrom;
import com.mark.web.stars.entity.po.Fragment;
import com.mark.web.stars.entity.vo.FragmentVo;
import com.mark.web.stars.service.IFragmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 碎片控制器
 * @Author: Mark
 * @CreateDate: 2019/6/2 17:49
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Api(tags = "星运-碎片模块")
@RestController
@RequestMapping(value = "/stars/fragment/")
public class FragmentController {

    private final IFragmentService iFragmentService;
    private final IUploadFilesService iUploadFilesService;

    @Autowired
    public FragmentController(IFragmentService iFragmentService, IUploadFilesService iUploadFilesService) {
        this.iFragmentService = iFragmentService;
        this.iUploadFilesService = iUploadFilesService;
    }

    @ApiOperation(value = "获取当前时间的信息：月份和天数")
    @GetMapping("getDateInfo")
    public ServerResponse<Map<String,Object>> getDateInfo(){
        Map<String, Object> map = DateTimeUtil.getMonthAndDay();
        return ServerResponse.model(map);
    }

    @ApiOperation(value = "获取碎片标签类型下拉框")
    @GetMapping("getTypeSelect")
    public ServerResponse<List<SelectVo<String,Integer>>> getTypeSelect(){
        List<SelectVo<String, Integer>> model = iFragmentService.getTypeSelect();
        return ServerResponse.model(model);
    }

    @ApiOperation(value = "获取碎片类型")
    @GetMapping("getTypeInfo")
    public ServerResponse<List<Map<String,Object>>> getTypeInfo(){
        List<Map<String, Object>> model = iFragmentService.getTypeInfo();
        return ServerResponse.model(model);
    }

    @ApiOperation(value = "获取搜索条件内的碎片信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前的页数",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",defaultValue = "50"),
            @ApiImplicitParam(name = "fkTypeId",value = "碎片所属的标签类型id",defaultValue = "")
    })
    @PostMapping(value = "getFragmentList")
    public ServerResponse<PageInfo<FragmentVo>> getFragmentList(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                                @RequestParam(value = "pageSize",defaultValue = "50") int pageSize,
                                                                @RequestParam(value = "fkTypeId") Integer fkTypeId){
        Map<String, Object> requestMap = Maps.newHashMap();
        requestMap.put("pageNum",pageNum);
        requestMap.put("pageSize",pageSize);
        requestMap.put("currentUserId",CurrentUser.getCurrentUserId());
        requestMap.put("fkTypeId",fkTypeId);
        return  iFragmentService.getFragmentList(requestMap);
    }

    @ApiOperation(value = "添加碎片信息")
    @PostMapping(value = "add")
    public ServerResponse<String> add(@Valid AddFragmentFrom addFragmentFrom, BindingResult result){
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        Fragment fragment = new Fragment();
        BeanUtils.copyProperties(addFragmentFrom,fragment);
        int currentUserId = CurrentUser.getCurrentUserId();
        fragment.setFkUserId(currentUserId);
        return iFragmentService.add(fragment);
    }

    @SuppressWarnings("unchecked")
    @PostMapping(value = "uploadImage")
    public ServerResponse<Map<String,String>> uploadFile(@RequestParam(value = "files", required = false) MultipartFile[] files, ServletRequest request) {
        final String modules = "fragment";
        String moduleId = Integer.toString(CurrentUser.getCurrentUserId());
        ServerResponse response = iUploadFilesService.uploadFiles(files, modules, moduleId);
        if(! response.isSuccess()){
           // return ServerResponse.createBySuccessMessage("上传图片失败！"+response.getMsg());
        }

        List<Map<String, String>> result  = (List<Map<String, String>>) response.getData();
        Map<String, String> urlMap = result.get(0);
        return ServerResponse.createBySuccess(urlMap);
    }

}
